package com.zust.zst.controller.score;

import com.zust.zst.common.dto.score.*;
import com.zust.zst.common.vo.ResultVO;

import com.zust.zst.score.service.ScoreService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gavin
 * @date 2022/11/10 20:55
 */

@RestController
@Api(tags = "成绩管理：学生成绩管理接口")
@Slf4j
public class ScoreController {

    @Resource(name = "scoreServiceImpl")
    private ScoreService scoreService;

    @RequestMapping(value = "/admin/scoreSelect/{page}/{size}",method = RequestMethod.GET)
    public ResultVO<List<Score>> adminScore(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        int currentPage = page;//当前是第几页
        int pageSize = size; //页面大小
        // ScoreVo scoreVo=new ScoreVo();
        List<Score> scoreList=scoreService.showAllStudentScore(((currentPage - 1) * pageSize),pageSize);
//        int totals = scoreService.showCountScore();
        if(scoreList!=null){

            return ResultVO.success("成绩信息",scoreList);
        }
        else return ResultVO.error("查询失败");
    }
    @RequestMapping(value = "/admin/scoreSelectCount",method = RequestMethod.GET)
    public ResultVO<Integer> adminScoreCount(){
        // ScoreVo scoreVo=new ScoreVo();
        int total=scoreService.getScoreCount();
//        int totals = scoreService.showCountScore();
        if(total>0){

            return ResultVO.success("成绩条数",total);
        }
        else return ResultVO.error("查询失败");
    }
    @RequestMapping(value = "/admin/scoreDelete/{id}",method = RequestMethod.GET)
    public ResultVO<String> adminScoreDelete(@PathVariable Integer id){
       int i= scoreService.deleteScore(id);
        return ResultVO.success("删除成功", null);
    }
    @RequestMapping(value = "/admin/getScoreById",method = RequestMethod.POST)
    public ResultVO<List<Score>> adminGetScoreById(@RequestBody GetScoreByIdDTO dto){
    List<Score> scores=scoreService.getScoreById(dto.getScoreId());
    if(scores!=null){
        return ResultVO.success(scores);
    }
    return ResultVO.error("查询失败");

    }
    @RequestMapping(value = "/admin/getScoreBySubId",method = RequestMethod.POST)
    public ResultVO<List<Score>> adminGetScoreById(@RequestBody GetScoreBySubIdDTO dto){
List<Score> scores=scoreService.getScoreBySubId(dto.getStudentSubjectId());
if(scores!=null){
    return  ResultVO.success(scores);
}
return ResultVO.error("查询失败");
    }
    @RequestMapping(value = "/admin/addScore",method = RequestMethod.POST)
    public ResultVO<String> addScore(@RequestBody AddScoreDTO addScoreDTO) {
        if(addScoreDTO.getFirst_score()>=60){
            int final_score= (int) ((addScoreDTO.getFirst_score()*0.5)+(addScoreDTO.getDaily_score()*0.5));
            addScoreDTO.setFinal_score(final_score);
        }
      else{
          addScoreDTO.setFinal_score(addScoreDTO.getSecond_score());
        }
      scoreService.addScore(addScoreDTO);
        log.info("插入成功：{}", addScoreDTO);
        return ResultVO.success("插入成功", null);
    }
    @RequestMapping(value = "/admin/updateScore",method = RequestMethod.POST)
    public ResultVO<String> updateScore(@RequestBody UpdateScoreDTO updateScoreDTO) {
        if (updateScoreDTO.getFirst_score()>=60){
            int final_score= (int) (updateScoreDTO.getFirst_score()*0.5+updateScoreDTO.getDaily_score()*0.5);
            updateScoreDTO.setFinal_score(final_score);
        }
        else {
            updateScoreDTO.setFinal_score(updateScoreDTO.getSecond_score());
        }
scoreService.updateScore(updateScoreDTO);
        log.info("编辑成功：{}", updateScoreDTO);
        return ResultVO.success("编辑成功",null);
    }
}
